package app.itgungnir.compose.wan.business.main.home

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import app.itgungnir.compose.wan.utils.jsonParser
import com.google.gson.reflect.TypeToken
import java.io.Serializable

@Entity
data class HomeBanner(
    @PrimaryKey
    val id: Long = 0L,
    val title: String? = null,
    val imagePath: String? = null,
    val url: String? = null
) : Serializable

@Entity
@TypeConverters(HomeArticleTagConverter::class)
data class HomeArticle(
    @PrimaryKey
    val id: Long = 0L,
    val title: String? = null,
    val desc: String? = null,
    val author: String? = null,
    val shareUser: String? = null,
    val chapterId: Long = 0L,
    val chapterName: String? = null,
    val superChapterId: Long = 0L,
    val superChapterName: String? = null,
    val collect: Boolean = false,
    val niceDate: String? = null,
    val niceShareDate: String? = null,
    val envelopePic: String? = null,
    val link: String? = null,
    val projectLink: String? = null,
    val type: Int = 0,
    val publishTime: Long = 0L,
    val tags: List<HomeArticleTag>? = null
) : Serializable {
    data class HomeArticleTag(
        val name: String? = null,
        val url: String? = null
    ) : Serializable
}

class HomeArticleTagConverter {
    @TypeConverter
    fun tagsToString(tag: List<HomeArticle.HomeArticleTag>?): String? = tag?.let { jsonParser.toJson(it) }

    @TypeConverter
    fun stringToTags(string: String?): List<HomeArticle.HomeArticleTag>? = string?.takeIf { it.isNotEmpty() }
        ?.let { jsonParser.fromJson(it, object : TypeToken<List<HomeArticle.HomeArticleTag>>() {}.type) }
}
